function show_draft_box(pageIndex) {
    var _box = $("#draft_box_window"), _list = $("#draft_box_list");
    _box.fadeIn(500);
    _list.html("<span class='load-tag'></span>");
    setTimeout(function () {
        pageIndex = typeof(pageIndex) == "undefined" ? 0 : pageIndex;
        var containerId = "draft_pagination";
        doRequest("/ajax/get_drafts", {pageIndex: pageIndex, stamptime: Math.random()}, false, function (resp) {
            var totalCount = resp.data.totalCount;
            var str = "";
            if (totalCount == 0) {
                str += "<div class='tip-null' style='height:160px;'></div>";
            } else {
                var htmlData = resp.data.htmlData;
                str += htmlData;
            }
            _list.html(str);
            createPaginationModel({
                containerId: containerId,
                perNum: 5,
                currIndex: resp.data.pageIndex,
                totalCount: totalCount,
                num: 6,
                flag: false
            }, show_draft_box);
        }, function (data) {
        });

    }, 1000);
}

function delete_get_drafts($id, obj) {
    var _show = $(obj).parents(".popup-box").find(".del-box-show");
    doGetRequest("/ajax/del_drafts", {draftsId: $id, stamptime: Math.random()}, false, function (resp) {
        $("#draft_number_box").html(resp.totalCount);
        show_draft_box(0);
        _show.text("删除成功").fadeIn(500);
        setTimeout(function () {
            _show.fadeOut(500);
        }, 1000);
    }, function (data) {
    });
}

$(function () {
    var tasks_validate_box = $("#tasks_validate_box"),tasks_cates_box=$("#tasks_cates_box"),tasks_tags_box = $("#tasks_tags_box");
    if (typeof(tasks_validate_box[0]) != "undefined") {
        $("body").delegate("#tasks_validate_box a", "click", function () {
            $(this).hasClass('on') ? $(this).removeClass('on') : $(this).addClass('on');
        });
    }
    if(typeof(tasks_cates_box[0])!="undefined"){
        if(tasks_cates_box.attr("data-flag") == 0){
            change_cate(tasks_cates_box.children("li:first").children('a').attr("date-id"));
            tasks_cates_box.children("li:first").addClass("on");
        }else{
            change_cate(tasks_cates_box.attr("data-flag"));
        }
        tasks_cates_box.find("li").click(function(){
            change_cate($(this).children('a').attr("date-id"));
            $(this).addClass("on").siblings().removeClass("on");
        });
    }
    if (typeof(tasks_tags_box[0]) != "undefined") {
        $("body").delegate("#tasks_tags_box a", "click", function () {
            $(this).hasClass('on') ? $(this).removeClass('on') : $(this).addClass('on');
            check_tasks_tags(tasks_tags_box);
        });
    }
    $("#seach_tasks_key_tag").blur(function (e) {
        var add_tags_t = $.trim($("#seach_tasks_key_tag").val());
        if(add_tags_t!='' && add_tags_t!='undefined' ){
            tasks_tags_box.append("<a href='javascript:void(0);' data-name='"+add_tags_t+"' class='on'>"+add_tags_t+"</a>");
            check_tasks_tags(tasks_tags_box);
            $("#seach_tasks_key_tag").val('');
            $(this).parents('.label-input').toggle(500);
        }

        // if (e.keyCode == 38 || e.keyCode == 40) {
        //     select_tag(e.keyCode);
        //     return;
        // } else if (e.keyCode == 13) {
        //     if (($("#seach_tasks_tag_box a.active"))[0] !== undefined) {
        //         selected_tag($("#seach_tasks_tag_box a.active"));
        //         return;
        //     }
        // }
        // var _self = $(this), _key_tag = _self.val(), _seach_tasks_tag_box = $("#seach_tasks_tag_box");
        // if (_key_tag == "") {
        //     _seach_tasks_tag_box.css("display", "none");
        //     return;
        // }
        // ;
        // _seach_tasks_tag_box.css("display", "block").html("<span class='load-tag'></span>");
        // doGetRequest("/ajax/seach_tasks_tag", {tag_key: _key_tag}, true, function (res) {
        //     var html = res.seachRes;
        //     if (html == "") {
        //         _seach_tasks_tag_box.html("<span class='empty'>匹配为空</span>");
        //     } else {
        //         setTimeout(function () {
        //             _seach_tasks_tag_box.html("").html(html);
        //         }, 500);
        //     }
        // }, function (res) {
        // });
    });
    // $("body").delegate("#seach_tasks_tag_box a", "click", function (e) {
    //     selected_tag($(this));
    // });

    function change_cate(cate_id) {

        $.ajax({
            type: 'POST',
            url: '/task/ajax_get_task_cate.html',//发送请求
            dataType : "json",
            success: function(result) {
                $("div[id^='task_']").hide();
                $.each(result, function(num, val) {
                    if(cate_id == val.cate_id){
                        $.each(val.cate_field, function(fn,field) {
                            $("#task_"+field).fadeTo(500,1);
                        });
                    }
                });
            }
        });
    }
    function check_tasks_tags(obj) {
        if ((obj.find("a[class='on']")).length > 2) {
            $("#tasks_tags_error").css("display", "block");
        } else {
            $("#tasks_tags_error").css("display", "none");
        }
    }

    function check_tasks_is_on(obj) {
        var t = 0;
        var tt = setInterval(function () {
            t++;
            $(obj).hasClass('on') ? $(obj).removeClass('on') : $(obj).addClass('on');
            if (t == 4) {
                clearInterval(tt);
            }
        }, 300);
        return;
    }

    //发布任务
    var _post_cateid_obj = $("#tasks_cates_box"),
        _post_plat_name_obj = $("#post_plat_name"),
        _post_logo_obj = $("#cover_id_cover_id"),
        _post_description_obj = $("#post_description"),
        _post_content_obj = $("#post_content"),
        _post_time_limit_obj = $("#post_time_limit"),
        _post_subnum_obj = $("#post_subnum"),
        _post_commission_obj = $("#post_commission"),
        _post_ups_commission_obj = $("#post_ups_commission"),
        _post_hiups_commission_obj = $("#post_hiups_commission"),
        _post_commission_per_obj = $("#post_commission_per"),
        _post_sponsorship_money_obj = $("#post_sponsorship_money"),
        _post_uid_obj = $("#post_uid"),
        _post_deposit_obj = $("#post_deposit"),
        _post_tid_obj = $("#post_tid"),
        _post_validate_obj = $("#tasks_validate_box"),
        _post_tags_obj = $("#tasks_tags_box"),

        _post_investment_money_obj = $("#post_investment_money"),
        _post_investment_money_amount_obj = $("#post_investment_money_amount"),
        _post_min_investment_money_obj = $("#post_min_investment_money"),
        _post_max_investment_money_obj = $("#post_max_investment_money"),
        _post_investment_day_obj = $("#post_investment_day"),
        _post_interest_rate_obj = $("#post_interest_rate"),
        _post_expected_money_obj = $("#post_expected_money"),
        _post_bonus_obj = $("#post_bonus"),
        _post_invite_code_obj = $("#post_invite_code"),
        _post_redirect_url_obj = $("#post_redirect_url"),
        _post_count_url_obj = $("#post_count_url"),
        _T_post_publish_btn = $("._T_post_publish");

    _T_post_publish_btn.on("click", function () {
        var _imglist = $("input[name='task_imglist[]']").map(function () {return $(this).val();}).get();
        var _self = $(this),
            _cate_id = check_task_cat(_post_cateid_obj),
            _plat_name = $.trim(_post_plat_name_obj.val()),
            _logo = $.trim(_post_logo_obj.val()),
            _description = $.trim(_post_description_obj.val()),
            _content = _post_content_obj.val(),
            _validate = check_validate(_post_validate_obj),
            _tags = check_tags(_post_tags_obj),

            _time_limit = $.trim(_post_time_limit_obj.val()),
            _subnum = $.trim(_post_subnum_obj.val()),
            _commission = $.trim(_post_commission_obj.val()),
            _ups_commission = $.trim(_post_ups_commission_obj.val()),
            _hiups_commission = $.trim(_post_hiups_commission_obj.val()),
            _commission_per = $.trim(_post_commission_per_obj.val()),
            _sponsorship_money = $.trim(_post_sponsorship_money_obj.val()),
            _investment_money = $.trim(_post_investment_money_obj.val()),
            _investment_money_amount = $.trim(_post_investment_money_amount_obj.val()),
            _min_investment_money = $.trim(_post_min_investment_money_obj.val()),
            _max_investment_money = $.trim(_post_max_investment_money_obj.val()),
            _investment_day = $.trim(_post_investment_day_obj.val()),
            _interest_rate = $.trim(_post_interest_rate_obj.val()),
            _expected_money = $.trim(_post_expected_money_obj.val()),
            _bonus = $.trim(_post_bonus_obj.val()),
            _invite_code = $.trim(_post_invite_code_obj.val()),
            _redirect_url = $.trim(_post_redirect_url_obj.val()),
            _count_url = $.trim(_post_count_url_obj.val()),
            _uid = $.trim(_post_uid_obj.val()),
            _tid = $.trim(_post_tid_obj.val()),
            _deposit = $.trim(_post_deposit_obj.val()),
            params = {}, _unvalid = _self.attr("data-unvalid"), _url = _self.attr("data-action-url");


        if (!check_plat_name(_plat_name) || !check_content(_content)) {return;};
        if(!_cate_id){layer.msg("至少选择一个任务分类哦",{icon:0,time:2000,shift:6,offset:300});return;}
        if(!_tags){layer.msg("至少选择一个任务标签哦",{icon:0,time:2000,shift:6,offset:300});return;}
        if(!_validate && _cate_id!=4){layer.msg("至少选择一个验证条件哦",{icon:0,time:2000,shift:6,offset:300});return;}
        if(!_subnum && _cate_id!=3){layer.msg("任务数量不能为空哦",{icon:0,time:2000,shift:6,offset:300});return;}
        if(!_count_url && _cate_id==4){layer.msg("统计链接不能为空哦",{icon:0,time:2000,shift:6,offset:300});return;}
        if(!_commission && _cate_id!=3){layer.msg("佣金不能为空哦",{icon:0,time:2000,shift:6,offset:300});return;}
        if((Number(_ups_commission)<0 || Number(_ups_commission) > 100) && _cate_id!=4){layer.msg("推荐人佣金百分比需在0-100",{icon:0,time:2000,shift:6,offset:300});return;}
        if((Number(_hiups_commission)<0 || Number(_hiups_commission) > 100) && _cate_id!=3){layer.msg("推荐人上级佣金百分比需在0-100",{icon:0,time:2000,shift:6,offset:300});return;}
        if(!_commission_per && _cate_id==3){layer.msg("佣金比例不能为空哦",{icon:0,time:2000,shift:6,offset:300});return;}

        params.cate_id = _cate_id;
        params.plat_name = _plat_name;
        params.logo = _logo;
        params.description = _description;
        params.content = _content;
        params.time_limit = _time_limit;
        params.subnum = _subnum;
        params.commission = _commission;
        params.ups_commission = _ups_commission;
        params.hiups_commission = _hiups_commission;
        params.commission_per = _commission_per;
        params.deposit = _deposit;
        params.sponsorship_money = _sponsorship_money;
        params.validate = _validate;
        params.tags = _tags;
        params.imglist = _imglist;
        params.investment_money = _investment_money;
        params.investment_money_amount = _investment_money_amount;
        params.min_investment_money = _min_investment_money;
        params.max_investment_money = _max_investment_money;
        params.investment_day = _investment_day;
        params.interest_rate = _interest_rate;
        params.expected_money = _expected_money;
        params.bonus = _bonus;
        params.invite_code = _invite_code;
        params.redirect_url = _redirect_url;
        params.count_url = _count_url;
        params.tid = _tid;
        _self.attr("disabled", "disabled").css("cursor", "not-allowed").val(_unvalid);
        doRequest(_url, params, true, function (data) {
            layer.confirm(data.errorMsg, {
                shift: 6, offset: 300, closeBtn: 0,
                btn: [data.word, '继续提交'], icon: 6, title: "小6提示" //按钮
            }, function () {
                window.location.href = data.url;
            }, function () {
                window.location.reload();
            });
        }, function (data) {
            _self.removeAttr("disabled").css("cursor", "pointer").val("确认发布");
            layer.msg(data.errorMsg, {icon: 0, time: 2000, offset: 300});
        });
    });
    _post_plat_name_obj.keyup(function(){
        var _self=$(this),_title=$.trim(_self.val());
        _total_len=parseInt(_title.length);
        $(".title-count").text(_total_len);
    });
    function check_task_cat(obj){
        obj = obj.find("li[class='on']");
        if(typeof(obj[0]) && obj.length > 0){
            return obj.children("a").attr("date-id");
        }else{
            return false;
        }
    }
    function check_tags(obj){
        var str="",obj = obj.find("a[class='on']");
        if(typeof(obj[0]) && obj.length > 0){
            obj.each(function(index,data){
                if((index+1) < 3){
                    str += $(this).attr("data-name")+",";
                }
            });
            return str.substring(0,str.length-1);
        }else{
            return "";
        }

    }
    function check_validate(obj){
        var str="",obj = obj.find("a[class='on']");
        if(typeof(obj[0]) && obj.length > 0){
            obj.each(function(index,data){
                if((index+1) < 6){
                    str += $(this).attr("data-tid")+",";
                }
            });
            return str.substring(0,str.length-1);
        }else{
            return "";
        }

    }
    function check_plat_name(str){
        var total_len,ch_len,en_len;
        if(str == ""){layer.msg("请输入平台名称",{icon:0,time:2000,shift:6,offset:300});return false;}
        total_len=parseInt(str.length);
        if((total_len) > 10 || (total_len) < 2){layer.msg("平台名称<span style='font-size:16px;color:#e66d61;'>必须在2到10个字之间</span>哦",{icon:0,time:2000,shift:6,offset:300});return false;}
        return true;
    }
    function check_content(str){
        if(str == ""){layer.msg("教程不能为空哦",{icon:0,time:2000,shift:6,offset:300});return false;}
        return true;
    }
    function select_tag(code){
        $("#seach_tasks_tag_box").focus();
        var tar = $("#seach_wtasks_tag_box a");
        if(tar[0] === undefined){return;}
        index = $("#seach_tasks_tag_box a.active").index();
        num = 0;
        if(code == 38){
            if(index < 1){return}
            num = index - 1;
        }else if(code == 40){
            if((index + 1) == tar.length){return;}
            num = index + 1;
        }
        var obj = tar.eq(num);
        obj.addClass("active").siblings().removeClass("active");
        $("#seach_tasks_tag_box").scrollTop(obj[0].scrollHeight * num);
    }

    function selected_tag(obj){
        var _self=obj,_flag=$("#tasks_tags_box").find("a[data-tid="+_self.attr("seach-tid")+"]");
        $("#seach_tasks_tag_box").html("").hide();$("#seach_tasks_key_tag").val("");
        if(typeof(_flag[0]) != "undefined"){
            if(_flag.hasClass("on")){
                check_tasks_is_on(_flag);
            }else{
                _flag.addClass("on");
            }
        }else{
            $("#tasks_tags_box").append("<a href='javascript:void(0);' data-name='"+_self.attr("seach-name")+"' data-tid='"+_self.attr("seach-tid")+"' class='on'>"+_self.attr("seach-name")+"</a>");
        }
        check_tasks_tags($("#tasks_tags_box"));
    }
});

